//
//  RegistViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/13.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "RegistViewController.h"

@interface RegistViewController ()
{
    BOOL agree;
}
@property (weak, nonatomic) IBOutlet UILabel *TitleLabel;
@property (weak, nonatomic) IBOutlet UIView *FirstLabel;
@property (weak, nonatomic) IBOutlet UIView *SecondLabel;
@property (weak, nonatomic) IBOutlet UIView *ThirdLabel;
@property (weak, nonatomic) IBOutlet UIView *FourthLabel;
@property (weak, nonatomic) IBOutlet UIView *FifthLabel;
@property (weak, nonatomic) IBOutlet UITextField *InviteCodeTF;
@property (weak, nonatomic) IBOutlet UITextField *PhoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *PasswordTF;
@property (weak, nonatomic) IBOutlet UITextField *RePasswordTF;
@property (weak, nonatomic) IBOutlet UITextField *CodeTF;
@property (weak, nonatomic) IBOutlet UIButton *CodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *RegistBtn;
@property (assign, nonatomic) NSInteger btnMark;
@property (weak, nonatomic) IBOutlet UIImageView *ReadImg;
@property (assign, nonatomic) NSInteger timeNmum;
@property (strong, nonatomic) NSTimer *getTime;
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _InviteCodeTF.keyboardType = UIKeyboardTypeNumberPad;
    _PhoneNumTF.keyboardType = UIKeyboardTypeNumberPad;
    _CodeTF.keyboardType = UIKeyboardTypeNumberPad;
    _btnMark = 1;
    _timeNmum = 60;
    [self createUI];
    [self judgeLoginEnabled];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_PhoneNumTF resignFirstResponder];
    [_PasswordTF resignFirstResponder];
    [_InviteCodeTF resignFirstResponder];
    [_RePasswordTF resignFirstResponder];
    [_CodeTF resignFirstResponder];
}

- (void)createUI{
    self.view.backgroundColor = [UIColor whiteColor];
    _TitleLabel.layer.borderColor = [[UIColor whiteColor]CGColor];
    _TitleLabel.layer.borderWidth = 1.0f;
    [_TitleLabel.layer setCornerRadius:15.0];
    _TitleLabel.layer.masksToBounds = YES;
    
    [_CodeBtn.layer setCornerRadius:15.0];
    _CodeBtn.layer.masksToBounds = YES;
    
    [_RegistBtn.layer setCornerRadius:20.0];
    _RegistBtn.layer.masksToBounds = YES;
    
    [self createLabel:_FirstLabel];
    [self createLabel:_SecondLabel];
    [self createLabel:_ThirdLabel];
    [self createLabel:_FourthLabel];
    [self createLabel:_FifthLabel];
}

- (void)createLabel:(UIView *)label{
    label.layer.borderColor = [[UIColor colorWithHex:0xebebeb]CGColor];
    label.layer.borderWidth = 1.0f;
    [label.layer setCornerRadius:17.0];
    label.layer.masksToBounds = YES;
}

-(void)judgeLoginEnabled{
    
    [_PhoneNumTF.rac_textSignal subscribeNext:^(NSNumber *result) {
        if(String(result).length == 11){
            [_CodeBtn setBackgroundColor:[UIColor redColor]];
            _CodeBtn.enabled = YES;
        }else{
            [_CodeBtn setBackgroundColor:[UIColor grayColor]];
            _CodeBtn.enabled = NO;
        }
        
    }];
}

- (IBAction)PassCodeClick:(UIButton *)sender {
    if (_PhoneNumTF.text.length == 0 || _PhoneNumTF.text.length != 11) {
        [self.view Message:@"请输入正确的手机号" HiddenAfterDelay:1];
        return;
    }
    
    if (_btnMark !=1){
        [self.view Message:@"请同意人民公社相关法律协议" HiddenAfterDelay:1];
        return;
    }
    if (_PhoneNumTF.text.length == 11){
        _timeNmum = 60;
        NSDictionary *dic = @{@"tel":_PhoneNumTF.text,@"type":@"7"};

        [THWManager GetHttp:API(GetCode) Parameters:dic Success:^(id responseObject) {
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
                    _getTime =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(begainTime) userInfo:nil repeats:YES];
                    _CodeBtn.enabled = NO;
                }else{
                    ALERT(responseObject[@"msg"]);
                }
            }
        } andFalse:^(NSError *error) {
        }];
    }
}

- (void)begainTime
{
    if (_timeNmum > 0) {
        _timeNmum--;
        _CodeBtn.enabled = NO;
        [_CodeBtn setTitle:[NSString stringWithFormat:@"%ldS",(long)_timeNmum] forState:UIControlStateNormal];
    }else{
        _timeNmum = 0;
        if ([_getTime isValid]) {
            [_getTime invalidate];
            _getTime = nil;
        }
        [_CodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        _CodeBtn.enabled = YES;
    }
}

- (IBAction)ReadProtolClick:(UIButton *)sender {
    if (agree) {
        _btnMark =1;
        [_ReadImg setImage:[UIImage imageNamed:@"Login_agree"]];
    }
    else {
        _btnMark = 2;
        [_ReadImg setImage:[UIImage imageNamed:@"Login_disagree"]];
    }
    agree = !agree;
}

- (IBAction)RegistClick:(UIButton *)sender {
    NSUUID *macUDID = [UIDevice currentDevice].identifierForVendor;
    NSString *test = [NSString stringWithFormat:@"%@", macUDID];
    if (_PhoneNumTF.text.length == 0 || _PhoneNumTF.text.length != 11) {
        [self.view Message:@"请输入正确的手机号" HiddenAfterDelay:1];
        return;
    }
    if (_PasswordTF.text.length == 0 || _RePasswordTF.text.length == 0) {
        [self.view Message:@"请输入密码" HiddenAfterDelay:1];
        return;
    }
    if (_PasswordTF.text.length < 6 || _RePasswordTF.text.length > 12) {
        [self.view Message:@"密码长度应在6-12位" HiddenAfterDelay:1];
        return;
    }
    if (_PasswordTF.text != _RePasswordTF.text) {
        [self.view Message:@"两次密码不一样" HiddenAfterDelay:1];
        return;
    }
    if (_CodeTF.text.length == 0) {
        [self.view Message:@"请输入验证码" HiddenAfterDelay:1];
        return;
    }
    if (_btnMark !=1){
        [self.view Message:@"请同意人民公社相关法律协议" HiddenAfterDelay:1];
        return;
    }
    if (_CodeTF.text.length == 0) {
        [self.view Message:@"请输入邀请码"];
        return;
    }
    [self.view loadingOnAnyView];
    NSDictionary *dic;
    NSData *PSD = [_PasswordTF.text
                   dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64PSD = [PSD base64EncodedStringWithOptions:0];
    if (_InviteCodeTF.text.length == 0) {
        dic = @{@"phone_ip":test,@"loginName":_PhoneNumTF.text,@"phoneCode":_CodeTF.text,@"loginPwd":base64PSD,@"reloginPwd":base64PSD};
    }else{
        dic = @{@"phone_ip":test,@"loginName":_PhoneNumTF.text,@"phoneCode":_CodeTF.text,@"loginPwd":base64PSD,@"reloginPwd":base64PSD,@"pid_invaperson_code":_InviteCodeTF.text};
    }
    [THWManager PostHttp:API(Regist) Parameters:dic Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
                [self.view Message:responseObject[@"msg"]];
                dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
                dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
                });
            }else{
                [self.view Message:responseObject[@"msg"]];
            }
        }
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
    
}

- (IBAction)BackClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
